<?php
	/*
	 * To change this template, choose Tools | Templates
	 * and open the template in the editor.
	*/
	
	function ismobile() {
        // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
        if(isset($_SERVER['HTTP_X_WAP_PROFILE']))
            return true;
        //此条摘自TPM智能切换模板引擎，适合TPM开发
        if(isset($_SERVER['HTTP_CLIENT'])&&'PhoneClient'==$_SERVER['HTTP_CLIENT'])
            return true;
        //如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
        if(isset($_SERVER['HTTP_VIA']))
            //找不到为flase,否则为true
            return stristr($_SERVER['HTTP_VIA'],'wap')?true:false;
        //判断手机发送的客户端标志,兼容性有待提高
        if(isset($_SERVER['HTTP_USER_AGENT'])){
            $clientkeywords=array('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile');
            //从HTTP_USER_AGENT中查找手机浏览器的关键字
            if(preg_match("/(".implode('|',$clientkeywords).")/i",strtolower($_SERVER['HTTP_USER_AGENT']))){
                return true;
            }
        }
        //协议法，因为有可能不准确，放到最后判断
        if(isset($_SERVER['HTTP_ACCEPT'])){
            // 如果只支持wml并且不支持html那一定是移动设备
            // 如果支持wml和html但是wml在html之前则是移动设备
            if((strpos($_SERVER['HTTP_ACCEPT'],'vnd.wap.wml')!== false)&&(strpos($_SERVER['HTTP_ACCEPT'],'text/html')===false||(strpos($_SERVER['HTTP_ACCEPT'],'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))){
                return true;
            }
        }
        return false;
    }
	
	/**
	+----------------------------------------------------------
	* 字符串截取，支持中文和其它编码
	+----------------------------------------------------------
	* @static
	* @access public
	+----------------------------------------------------------
	* @param string $str 需要转换的字符串
	* @param string $start 开始位置
	* @param string $length 截取长度
	* @param string $charset 编码格式
	* @param string $suffix 截断显示字符
	+----------------------------------------------------------
	* @return string
	+----------------------------------------------------------
	*/
	function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
	{
		if(function_exists("mb_substr"))
		{
			if($suffix)
			{
				return mb_substr($str, $start, $length, $charset)."...";
			}
			else
			{
				return mb_substr($str, $start, $length, $charset);
			}
		}
		elseif(function_exists('iconv_substr'))
		{
			if($suffix)
			{
				return iconv_substr($str,$start,$length,$charset)."...";
			}
			else
			{
				return iconv_substr($str,$start,$length,$charset);
			}
		}
		$re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
		$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
		$re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
		$re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
		preg_match_all($re[$charset], $str, $match);
		$slice = join("",array_slice($match[0], $start, $length));
		if($suffix)
		{ 
			return $slice."...";
		}
		else
		{
			return $slice;
		}
	}

	function format_num($str,$ct)
	{
		return sprintf("%.2f", $str);
	}

	function number($num,$status=NULL){
		$db_library_main=M('db_library_main');
		$db_assist_apply=M('db_assist_apply');
		$db_project=M('db_project');
		$db_universal_audit=M('db_universal_audit');
		$db_project_plan=M('db_project_plan');
		$db_schedule=M('db_schedule');
		if($_SESSION['dms_uid']!=1){
			switch ($num)
			{
				case 1:
					$where['status']=-1;
					$num1=$db_library_main->field('id')->where($where)->count();
					if(($num1>0) and ($num1<=99))
					{
						$result=$num1;
					}
					elseif($num1>99)
					{
						$result="99+";
					}
					$result="<span class='num-label'>".$result."</span>";
					return $result;
					break;
				case 2:
					//协作申请审核
					//项目负责人
					$all=array();
					$where['manager_uid'] = $_SESSION['dms_uid'];
					$project_result = $db_project->field('id')->where($where)->find();
					if(!empty($project_result)){
						$where_zj['db_project.manager_uid'] = $_SESSION['dms_uid'];
						$where_zj['db_assist_apply.status']=1;
						$p1=$db_assist_apply->join('Left Join db_project on db_project.id = db_assist_apply.pid')->where($where_zj)->select();
						$n1=count($p1);
					}
					//中心营销部负责人
					$where['type']=1;
					$where['_string']='find_in_set("'.$_SESSION['dms_uid'].'",audit_uid)';
					$where['audit_name']='营销部负责人';
					$job_result=$db_universal_audit->where($where)->select();
					foreach($job_result as $key=>$value){
						if($key!=0){$zx.=','.$value['audit_type'];}else{$zx.=$value['audit_type'];}
					}
					if(!empty($zx)){
						$where_zx['db_universal_audit.audit_type']=array('in',$zx);
						$where_zx['db_universal_audit.type']=1;
						$where_zx['db_assist_apply.status']=2;
						$p2=$db_assist_apply->join('Left Join db_project on db_project.id = db_assist_apply.pid')->join('Left Join db_universal_audit on db_universal_audit.audit_type=db_assist_apply.audit_type')->where($where_zx)->group('db_assist_apply.id')->select();
						$n2=count($p2);
					}
					$num15=$n1+$n2;
					if(($num15<=99) and ($num15>0)){
						$xzsq="<span class='num-label'>".$num15."</span>";
					}
					elseif($num15>99){
						$xzsq="<span class='num-label'>(99+)</span>";
					}
					//根据第二参数返回导航或列表数组
					if($status==1){
						$nav_arr[1]=$n1;
						$nav_arr[2]=$n2;
						return $nav_arr;
					}elseif($status==2){
						$all = array_merge((array)$p1,(array)$p2);
						return $all;
					}else{
						return $xzsq;
					}
					break;
				case 3:
					//营销计划管理
					$where_audit['type']=2;
					//是否营销负责人
					$where_audit['audit_name']='营销负责人';
					$where_audit['_string']='find_in_set("'.$_SESSION['dms_uid'].'",audit_uid)';
					$result_audit=$db_universal_audit->where($where_audit)->find();
					$uid_arr = explode(',',$result_audit['audit_uid']);
					if(in_array($_SESSION['dms_uid'],$uid_arr))
					{
						$where_yx['db_project_plan.status']=1;
						$where_yx['db_universal_audit.type']=2;
						$where_yx['db_universal_audit.audit_name']='营销负责人';
						$where_yx['db_universal_audit.audit_user']=array('like',"%".$_SESSION['dms_username']."%");
						$p1=$db_project_plan->join('Left Join db_project on db_project.id = db_project_plan.pid')->join('LEFT JOIN db_universal_audit ON db_project_plan.audit_type=db_universal_audit.audit_type')->where($where_yx)->group('db_project_plan.id')->select(); 
						$pa1=count($p1);
					}
					//是否运营负责人
					$where_audit['audit_name']='运营负责人';
					$where_audit['_string']='find_in_set("'.$_SESSION['dms_uid'].'",audit_uid)';
					$result_audit=$db_universal_audit->where($where_audit)->find();
					$uid_arr = explode(',',$result_audit['audit_uid']);
					if(in_array($_SESSION['dms_uid'],$uid_arr))
					{
						$where_yy['db_project_plan.status']=2;
						$where_yy['db_universal_audit.type']=2;
						$where_yy['db_universal_audit.audit_name']='运营负责人';
						$where_yy['db_universal_audit.audit_user']=array('like',"%".$_SESSION['dms_username']."%");
						$p2=$db_project_plan->join('Left Join db_project on db_project.id = db_project_plan.pid')->join('LEFT JOIN db_universal_audit ON db_project_plan.audit_type=db_universal_audit.audit_type')->where($where_yy)->group('db_project_plan.id')->select(); 
						$pa2=count($p2);
					}
					//是否公司负责人
					$where_audit['audit_name']='公司负责人';
					$where_audit['_string']='find_in_set("'.$_SESSION['dms_uid'].'",audit_uid)';
					$result_audit=$db_universal_audit->where($where_audit)->find();
					$uid_arr = explode(',',$result_audit['audit_uid']);
					if(in_array($_SESSION['dms_uid'],$uid_arr))
					{
						$where_gs['db_project_plan.status']=3;
						$where_gs['db_universal_audit.type']=2;
						$where_gs['db_universal_audit.audit_name']='公司负责人';
						$where_gs['db_universal_audit.audit_user']=array('like',"%".$_SESSION['dms_username']."%");
						$p3=$db_project_plan->join('Left Join db_project on db_project.id = db_project_plan.pid')->join('LEFT JOIN db_universal_audit ON db_project_plan.audit_type=db_universal_audit.audit_type')->where($where_gs)->group('db_project_plan.id')->select(); 
						$pa3=count($p3);
					}
					$num3=$pa1+$pa2+$pa3;
					if(($num3<=99) and ($num3>0)){
						$yxjh="<span class='num-label'>".$num3."</span>";
					}
					elseif($num3>99){
						$yxjh="<span class='num-label'>(99+)</span>";
					}
					//根据第二参数返回导航或列表数组
					if($status){
						$nav_arr[1]=$pa1;
						$nav_arr[2]=$pa2;
						$nav_arr[3]=$pa3;
						return $nav_arr;
					}else{
						return $yxjh;
					}
					break;
				case 4:
					$db_relationship=M('db_relationship');
					$where_r['leader_uid']=$_SESSION['dms_uid'];
					$result_r=$db_relationship->where($where_r)->find();
					if($result_r){
						$person_uid=$result_r['person_uid'];
					}else{
						$person_uid='NULL';
					}
					$Model=M();
					$str='SELECT db_audit_main.* FROM `db_audit_main` LEFT JOIN db_universal_audit ON db_audit_main.status=db_universal_audit.audit_sort and db_universal_audit.type=3 and db_universal_audit.audit_type=130005 WHERE db_audit_main.type=3 and (find_in_set("'.$_SESSION['dms_uid'].'",audit_uid) and db_audit_main.status>0) UNION ALL SELECT db_audit_main.* FROM `db_audit_main` LEFT JOIN db_universal_audit ON db_audit_main.status=db_universal_audit.audit_sort and db_universal_audit.type=5 and db_audit_main.audit_type=db_universal_audit.audit_type WHERE db_audit_main.type=5 and (find_in_set("'.$_SESSION['dms_uid'].'",audit_uid) and db_audit_main.status>0) UNION ALL (SELECT db_audit_main.* FROM `db_audit_main` Left Join db_project on db_project.id=db_audit_main.pid LEFT JOIN db_universal_audit ON db_audit_main.audit_type=db_universal_audit.audit_type WHERE ( ( ((((db_project.manager_uid="'.$_SESSION['dms_uid'].'") and (db_audit_main.status=1) or (db_audit_main.status=2)) or (find_in_set("'.$_SESSION['dms_uid'].'",audit_uid) and db_audit_main.status=2)) and (db_audit_main.type=4)) ) ) GROUP BY db_audit_main.id) UNION ALL (SELECT db_audit_main.* FROM `db_audit_main` Left Join db_project on db_project.id=db_audit_main.pid LEFT JOIN db_universal_audit ON db_audit_main.audit_type=db_universal_audit.audit_type WHERE ( ((db_project.manager_uid="'.$_SESSION['dms_uid'].'") and (((audit1_uid="'.$_SESSION['dms_uid'].'") and ((db_audit_main.status=2) or (db_audit_main.status=3))) or (db_audit_main.status=1))) or ( (find_in_set("'.$_SESSION['dms_uid'].'",audit_uid)) and (((audit2_uid="'.$_SESSION['dms_uid'].'") and ((db_audit_main.status=3))) or (db_audit_main.status=2))) ) and (db_audit_main.type=1) GROUP BY db_audit_main.id) UNION ALL SELECT db_audit_main.* FROM `db_audit_main` LEFT JOIN db_universal_audit ON db_audit_main.status=db_universal_audit.audit_sort and db_universal_audit.type=6 WHERE db_audit_main.type=6 and (find_in_set("'.$_SESSION['dms_uid'].'",audit_uid) and db_audit_main.status>0)  UNION ALL SELECT db_audit_main.* FROM `db_audit_main` LEFT JOIN db_universal_audit ON db_audit_main.status=db_universal_audit.audit_sort and db_universal_audit.type=7 WHERE db_audit_main.type=7 and ((find_in_set("'.$_SESSION['dms_uid'].'",audit_uid) and db_audit_main.status>1) or (db_audit_main.status=1 and add_uid in('.$person_uid.')))  ORDER BY id desc';
					$count=$Model->query($str);
					foreach($count as $kk=>$vv){
						if($vv['type']==5){
							$db_universal_audit=M('db_universal_audit');
							$where['type']=$vv['type'];
							if(!empty($vv['audit_type'])){$where['audit_type']=$vv['audit_type'];}
							if($sort=='gt'){
								$where['audit_sort']=array('gt',$vv['status']);
								$order='audit_sort';
							}
							elseif($sort=='lt'){
								$where['audit_sort']=array('lt',$vv['status']);
								$order='audit_sort desc';
							}
							else{
								$where['audit_sort']=$vv['status'];
							}
							$where['audit_uid']=array('exp','IS NOT NULL');
							$audit_user=$db_universal_audit->where($where)->order($order)->find();
							$audit_arr=explode(',',$audit_user['audit_uid']);
							if(!in_array($_SESSION['dms_uid'],$audit_arr)){
								unset($count[$kk]);
							}
						}
					}
					$num4=count($count);
					if(($num4>0) and ($num4<=99))
					{
						$result=$num4;
					}
					elseif($num4>99)
					{
						$result="99+";
					}
					$result="<span class='num-label'>".$result."</span>";
					return $result;
					break;
			}
		}
	}

	function get_center_name($id){
		if($id==130004){
			$name="建设管理";
		}elseif($id==130005){
			$name="运营管理";
		}elseif($id==130003){
			$name="拓展中心";
		}elseif($id==130011){
			$name="景翼";
		}
		return $name;
	}
	
	function is_permission($num1,$num2,$num3){
		$permissions_rule=$_SESSION['dms_permissions_rule'];
		switch($num2){
			case 1:
				$num2=array('U1','U2','U3');
			break;
			case 2:
				$num2=array('U2','U3');
			break;
			case 3:
				$num2=array('U3');
			break;
			default:
				$num2=array('U1','U2','U3','U0');
			break;
		}
		switch($num3){
			case 1:
				$num3=array('Q1','Q2','Q3');
			break;
			case 2:
				$num3=array('Q2','Q3');
			break;
			case 3:
				$num3=array('Q3');
			break;
			case 4:
				$num3=array('Q0');
			break;
			case 5:
				$num3=array('QQ');
			break;
			default:
				$num3=array('Q1','Q2','Q3','Q0');
			break;
		}
		$bool=0;
		foreach($num2 as $key=>$value) 
		{
			foreach($num3 as $kk=>$vv) 
			{
				if(preg_match("/".$num1.'_'.$value.'_'.$vv."/",$permissions_rule))
				{
					$bool=1;
					break;
				}
			}
		}
		return $bool;
	}
	
	function get_permission($num1,$num2,$num3){
		$permissions_rule=$_SESSION['dms_permissions_rule'];
		switch($num2){
			case 1:
				$num2=array('U1');
			break;
			case 2:
				$num2=array('U2');
			break;
			case 3:
				$num2=array('U3');
			break;
			default:
				$num2=array('U1','U2','U3','U0');
			break;
		}
		switch($num3){
			case 1:
				$num3=array('Q1');
			break;
			case 2:
				$num3=array('Q2');
			break;
			case 3:
				$num3=array('Q3');
			break;
			case 4:
				$num3=array('Q0');
			break;
			case 5:
				$num3=array('QQ');
			break;
			default:
				$num3=array('Q1','Q2','Q3','Q0');
			break;
		}
		$bool=0;
		foreach($num2 as $key=>$value) 
		{
			foreach($num3 as $kk=>$vv) 
			{
				if(preg_match("/".$num1.'_'.$value.'_'.$vv."/",$permissions_rule))
				{
					$bool=1;
					break;
				}
			}
		}
		return $bool;
	}
	
	//是否有项目内部功能权限 designed by:zhangsen 2017-09-29
	function is_project($pid,$fid){
		$db_project=M('db_project');
		$where_p['id']=$pid;
		$project=$db_project->where($where_p)->find();
		$db_project_function=M('db_project_function'); 
		$where['uid']=$_SESSION['dms_uid'];
		$where['pid']=$pid;
		$where['_string']="find_in_set(".$fid.",p_ids)";
		$where['start_date']=array('elt',date("Y-m-d"));
		$where['end_date']=array('egt',date("Y-m-d"));
		$count=$db_project_function->where($where)->count();
		if($count>0){
			return 1;
		}
	}
	
	//是否有项目内部功能权限 designed by:zhangsen 2017-10-24
	function is_project_function($pid,$fid){
		$db_project=M('db_project');
		$where_p['id']=$pid;
		$project=$db_project->where($where_p)->find();
		$db_project_function=M('db_project_function'); 
		$where['uid']=$_SESSION['dms_uid'];
		$where['pid']=$pid;
		$where['_string']="find_in_set(".$fid.",p_ids)";
		$where['start_date']=array('elt',date("Y-m-d"));
		$where['end_date']=array('egt',date("Y-m-d"));
		$count=$db_project_function->where($where)->count();
		//echo $db_project_function->getLastSql();die;
		if(empty($project['info_id'])){
			if((preg_match('/XM_U1/',$_SESSION['dms_permissions_rule'])) || ((in_array($project['center_id'],$_SESSION['dms_select_id'])) && preg_match('/XM_U2/',$_SESSION['dms_permissions_rule'])) || ($_SESSION['dms_uid']==$project['add_uid'] && preg_match('/XM_U3/',$_SESSION['dms_permissions_rule'])) || ($_SESSION['dms_uid']==$project['manager_uid']) || ($_SESSION['dms_uid']==$project['director_uid']) || $count>0){
				return 1;
			}
		}else{
			if((preg_match('/SW_U1/',$_SESSION['dms_permissions_rule'])) || ((in_array($project['center_id'],$_SESSION['dms_sw_select_id'])) && preg_match('/SW_U2/',$_SESSION['dms_permissions_rule'])) || ($_SESSION['dms_uid']==$project['add_uid'] && preg_match('/SW_U3/',$_SESSION['dms_permissions_rule'])) || ($_SESSION['dms_uid']==$project['manager_uid']) || ($_SESSION['dms_uid']==$project['director_uid']) || $count>0){
				return 1;
			}
		}
	}
	
	//是否是项目经理 designed by:zhangsen 2017-09-29
	function is_project_manager(){
		$db_project=M('db_project');
		$where_p['manager_uid']=$_SESSION['dms_uid'];
		$count=$db_project->where($where_p)->count();
		if($count>0){
			return 1;
		}
	}
	
	//是否参与进行中项目 designed by:zhangsen 2017-11-14
	function is_assist(){
		$project=array();
		$db_project=M('db_project');
		$where_p['_string']="(manager_uid=".$_SESSION['dms_uid'].") or (director_uid=".$_SESSION['dms_uid'].")";
		$where_p['status']=1;
		$pro=$db_project->where($where_p)->group('id')->select();
		foreach($pro as $ka=>$va){
			$project[]=$va['id'];
		}
		$db_project_bg=M('db_project_bg');
		$where_bg['_string']='find_in_set("'.$_SESSION['dms_uid'].'",project_uids)';
		$where_bg['status']=1;
		$bg=$db_project->field('db_project.*')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->where($where_bg)->select();
		foreach($bg as $ka=>$va){
			$project[]=$va['id'];
		}
		$db_project_user=M('db_project_user');
		$where_u['uid']=$_SESSION['dms_uid'];
		$where_u['pid']=array('exp','is not NULL');
		$where_u['_string']="(('".date("Y-m-d")."'>=come_date and '".date("Y-m-d")."'<`out_date`) or ('".date("Y-m-d")."'>=come_date and out_date is NULL))";
		$puser=$db_project_user->where($where_u)->group('pid')->select();
		foreach($puser as $ka=>$va){
			$project[]=$va['pid'];
		}
		$project=array_unique($project);
		rsort($project);
		$where_ps['id']=array('in',join(',',$project));
		$where_ps['status']=1;
		if($_SESSION['dms_uid']==1){unset($where_ps);$where_ps['status']=1;}
		$project_list=$db_project->where($where_ps)->order('id desc')->find();
		if(!empty($project_list)){
			return 1;
		}
	}
	
	//是否参与进行中的运营项目 designed by:zhangsen 2017-11-14
	function is_assist_yy(){
		$db_project_user=M('db_project_user');
		$db_project=M('db_project');
		$where_p['_string']="(manager_uid=".$_SESSION['dms_uid'].") or (director_uid=".$_SESSION['dms_uid'].")";
		$where_p['status']=1;
		$where_p['center_id']=130005;
		$where_p['info_id']=array('exp','is NULL');
		$pro=$db_project->where($where_p)->group('id')->select();
		foreach($pro as $ka=>$va){
			$project[]=$va['id'];
		}
		$where_u['uid']=$_SESSION['dms_uid'];
		$where_u['pid']=array('exp','is not NULL');
		$where_u['_string']="(('".date("Y-m-d")."'>=come_date and '".date("Y-m-d")."'<`out_date`) or ('".date("Y-m-d")."'>=come_date and out_date is NULL))";
		$puser=$db_project_user->where($where_u)->group('pid')->select();
		foreach($puser as $ka=>$va){
			$project[]=$va['pid'];
		}
		$project=array_unique($project);
		rsort($project);
		$where_ps['id']=array('in',join(',',$project));
		$where_ps['status']=1;
		if($_SESSION['dms_uid']==1){unset($where_ps);$where_ps['status']=1;$where_ps['center_id']=130005;$where_ps['info_id']=array('exp','is NULL');}
		$project_list=$db_project->where($where_ps)->order('id desc')->find();
		if(!empty($project_list)){
			return 1;
		}
	}
	
	//经营数据权限 designed by:zhangsen 2018-01-19
	function is_operat_stage_update($pid){
		//判断经营项目项目阶段能否编辑
		$db_project_operate=M('db_project_operate');
		$db_project_operate_detail=M('db_project_operate_detail');
		$db_project_operate_data=M('db_project_operate_data');
		$where_o['pid']=$pid;
		$opera=$db_project_operate->where($where_o)->select();
		foreach($opera as $ko=>$vo){
			$where_detail['mid']=$vo['id'];
			$where_detail['fid']=33;
			$jysj_detail=$db_project_operate_detail->where($where_detail)->group('sid')->find();
			if(!empty($jysj_detail)){$is_up=1;}
		}
		//新数据
		$where_n['pid']=$pid;
		$n_detail=$db_project_operate_data->where($where_n)->find();
		if(!empty($n_detail)){$is_up=1;}
		return $is_up;
	}

    //获取指定日期之后的指定工作日后的日期 designed by:wuhuili 2018-5-22
    function get_days($days){
        $db_work_day=M('db_work_day');
        $date=date('Y-m-d',time());
        $days_where['date']=array('egt',$date);
        $days_where['type']=1;
        $days=$db_work_day->where($days_where)->limit($days)->select();
        $last_day=end($days);
        return $last_day['date'];
    }

    //获取待领取的部门任务单 designed by:wuhuili 2018-5-24
    function get_receive_task(){
        $db_department_task=M('db_department_task');
        $select_where['db_department_task_user.uid']=$_SESSION['dms_uid'];
        $select_where['db_department_task_user.type']=1;
        $select_where['db_department_task.status']=2;
        $count=$db_department_task->join('LEFT JOIN db_department_task_user ON db_department_task_user.tid=db_department_task.id')->where($select_where)->count();
        return $count;
    }
?>